package com.bai;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.AlipayConfig;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.domain.AlipayTradeCreateModel;
import com.alipay.api.request.AlipayTradeCreateRequest;
import com.alipay.api.response.AlipayTradeCreateResponse;

public class PayTest {
    public static final String URL = "https://openapi.alipay.com/gateway.do";
    public static final String APP_ID = "2021005156674866";
    public static final String PRIVATE_KEY = "MIIEuwIBADANBgkqhkiG9w0BAQEFAASCBKUwggShAgEAAoIBAQC9GkeK2YQg5TWVsnaCP68yds8RszmTJy/IcWjIXueUIBiGma4C/Exl4qKJ96/BoasDLEGDfmqvZOGfXFZM6IeItd6507xabmBltvjxcZzdeNa8ZlJJEPVgCL6+v22RzXxDIDPzJs682RXwECryvB1GOJ2Y5oeAICQ61Ze6gQg1nC8fFLo6cY073YYHbrrdfLi2QJRa9faN7jWsG8sRmknJkqILzjYFO2xX402GgrAOWGnGfmJboD+WVNExytOKxcdjuiFNWYswMWjnI25C1uJ7coyI06m7lE+oC/NliJOLiopETeL92LGsKya+H14bo3nv4V/KZiRL9FCSr9V1CzP9AgMBAAECggEBALOTMBsoAjQm1eLpTWbUyS+X6X26f9NA5DxpjgKxNSLR89hmOfv10SrztDxmbI3pU3e44CHZu8ABgcyOqBlyu7pUU0SOUJBjvsD4p6DIrhkW9nyEgmGGbC2dQF3tdUR/KSCphQvDb5GgaRfGkrJdd0NG4knfqQ8/9oe0a9idTclxegdmakEGQCpahrvXlLZbCzENjbGFMxokji7NGIBfHQOuSAmjOfpg8gYnuASj3vPjeAAbctm2MAJGcpma4XB5OOat6m+krqoCoOakPEPWEaJbQZK0W6LrF4LRIQT3Yh2c0pp53LonNsuYT0N6D4vFRjdASQlCSxqiycPbzUo/lUECgYEA9z/VUMzHDQ35/HPsTroa9nR6JPaVWvOOKVEqI84sBLKPjpuX1sJX/XawDYzAfLwNQuc3XChFYN96o6cI3t3S12EVVg5H0/W8LSkfTAy8wr9kCZlDgzaaVGJy5xC1qs6BDtMPGBTt4P3e1stS4nZ3aQYgWPsHY9GEHtMMbMl8ddECgYEAw8ud1UsYNq+ft/+9s/rO0gHEVY4mWchOqdyOrbpRqdZObmQVurvGR44F+xP/qkmK49uU6647RH7PEWj0nNPVsH6+LXCsNhFMMrMl2yOWCQsJAxglhkmVaQXgxVMhr3Auh/RH+RVI+eTqepKiJscCkdnSEk8ZbkA5rFKF2Vyc6m0CfyeZEZDfOwHcOy0zY6U4KRlPjkBdPY5Tc3EqjpUtb99Qua0dPjQm6/CsEYYaBLn1ZCpS3Yrk6Debkwiit8j5vcrAwaoGn3pS4NBaxahoYdLO1Da1VRqupAjfu4dGJdI1QHYfV0uEDpT6E7ZS2fNrBbTMRmH50jUqWP6Wzmad3lECgYAFBB9sCnt5UzCFV0D17nQD/1WXV4f1/uK9CVCCdOaIslvaCihgW1UuGh5fG1kOdMnhIowDKbiM/a6yZHz305mC5wxaP6u5yuGn3t0Wayf53VWCxciYUyRHx4XFqGaRhw86HnEFmZ0AAfRb5YvP8INJV0pBe9NL9stDde3F7tB6VQKBgAm8VcQ5laz+XGfJQoFWIMK+BDBXvhTfjBtQv09Hpc1AAxG4/j0DhTwBu2XaT8g7isqZGrjzKS2HvoELE7WTXiH+78XHfBZ3WJ+7/JYsEhKD5hWhdzK5qYd7cWyHsh1HShkRRpJkFIe5ts7sZs72e5Oz7Fu7OzDWpxlTBosI6r/3";

    public static final String FORMAT = "json";
    public static final String CHARSET = "utf-8";
    public static final String ALIPAY_PUBLIC_KEY = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAiSAM5juc9xpSmlo5Gg3BG1j1pPFCi60riR4Msb93umDBOlJdo2GibLQ2wc1jyg8FRWFI4ndadEESzFhimaYORSZbnb37gX7bvvirGW8KdNV28p5LD3ro/Q4e0ZEr1s67RjdD6vbV7OQ6UBYr2t89KxVEC6Rv/TpYi47WcsTOBZkxltPDq0G83UDTfKNZ0YlXJLVulxpjBUOWzfxow4JRpLqCWeQ4NzofYuJMwvK7zIeFeQEq4QNQXtnzQU3Vhz6/FIfHv9DnD5nArbZrIrzJDmgKBSrvoZHzCw7YteXoUGQgwnvD5PIonLWl3zg3fKzFs93AWQFY6fkSTvL1evu77wIDAQAB";
    public static final String SIGN_TYPE = "RSA2";

    public static void main(String[] args) throws AlipayApiException {
        AlipayConfig alipayConfig = new AlipayConfig();
        alipayConfig.setServerUrl(URL);
        alipayConfig.setAppId(APP_ID);
        alipayConfig.setPrivateKey(PRIVATE_KEY);
        alipayConfig.setFormat("json");
        alipayConfig.setCharset(CHARSET);
        alipayConfig.setAlipayPublicKey(ALIPAY_PUBLIC_KEY);
        alipayConfig.setSignType(SIGN_TYPE);
        AlipayClient alipayClient = new DefaultAlipayClient(alipayConfig);

        //---------------

//        // 构造请求参数以调用接口
//        AlipaySystemOauthTokenRequest request = new AlipaySystemOauthTokenRequest();
//
//        // 设置刷新令牌
//        request.setRefreshToken("201208134b203fe6c11548bcabd8da5bb087a83b");
//
//        // 设置授权码
//        request.setCode("8414e05b698b439db80876d3cd3eEX36");
//
//        // 设置授权方式
//        request.setGrantType("authorization_code");
//
//        AlipaySystemOauthTokenResponse response = alipayClient.execute(request);
//        System.out.println(response.getBody());
//        if (response.isSuccess()) {
//            System.out.println("调用成功");
//        } else {
//            System.out.println("调用失败");
//            // sdk版本是"4.38.0.ALL"及以上,可以参考下面的示例获取诊断链接
//            // String diagnosisUrl = DiagnosisUtils.getDiagnosisUrl(response);
//            // System.out.println(diagnosisUrl);
//        }
        AlipayTradeCreateRequest request1 = new AlipayTradeCreateRequest();
        AlipayTradeCreateModel model = new AlipayTradeCreateModel();

        // 设置商户订单号
        model.setOutTradeNo("201503200101010010");

        // 设置订单总金额 必须
        model.setTotalAmount("10");

        // 设置订单标题 必须
        model.setSubject("Ipho88ne6 16G");

        // 设置产品码
        model.setProductCode("JSAPI_PAY");

        // 设置卖家支付宝用户ID
        model.setSellerId("2088532804946364");

        // uid参数未来计划废弃，存量商户可继续使用，新商户请使用openid。请根据应用-开发配置-openid配置选择支持的字段。 必须
//        model.setBuyerId("8414e05b698b439db80876d3cd3eEX36");
//        model.setBuyerOpenId("036_sx8iOuBwFoDn7_HA-ugrx5DKS8xIaqy9u1nGLCY3T40");
        model.setBuyerOpenId("038_sx8iOuBwFoDn7_HA-ugrxRERB3-qtFwS3p-ZNHdaCgb");

        // 设置订单附加信息
        model.setBody("Iphone6 16G");


        // 设置订单绝对超时时间
        model.setTimeExpire("2025-12-31 10:05:00");

        // 设置订单相对超时时间
        model.setTimeoutExpress("90m");

        request1.setBizModel(model);
        // 第三方代调用模式下请设置app_auth_token
        // request.putOtherTextParam("app_auth_token", "<-- 请填写应用授权令牌 -->");

        AlipayTradeCreateResponse response1 = alipayClient.execute(request1);
        System.out.println(response1.getBody());

        if (response1.isSuccess()) {
            System.out.println("调用成功");
        } else {
            System.out.println("调用失败");
            // sdk版本是"4.38.0.ALL"及以上,可以参考下面的示例获取诊断链接
            // String diagnosisUrl = DiagnosisUtils.getDiagnosisUrl(response);
            // System.out.println(diagnosisUrl);
        }


    }


}
